<template>
  <base-breadcrumb>
    <a-card>
      <a-button class="mb12" type="primary" @click="add" v-if="hideBuquBtn()">新增</a-button>
<!--      <a-button class="mb12 ml12" type="primary" @click="$refs.table.refresh()">刷新</a-button>-->
      <list-table
        ref="table"
        :columns="columns"
        :request="loadData"
        :isScrollX="false"
        :page-keys="['pageNo','pageSize','totalSize']"
        :show-page="false"
      >
        <div slot="actions" slot-scope="text, record" class="pub-table-list-action">
          <span @click="edit(record)" v-if="hideBuquBtn()">编辑</span>
          <span v-if="record.status*1===0&&hideBuquBtn()" @click="del(record)">删除</span>
        </div>
      </list-table>
    </a-card>
    <add class="vue" :visible.sync="visible" :form="form" @ok="$refs.table.refresh()"></add>
  </base-breadcrumb>
</template>

<script>
import api from '@/api/safetySupervision/base/postInfo'
import add from './add'

export default {
  name: 'a' + Date.now(),
  components: {
    add
  },
  data() {
    return {
      form: {},
      visible: false,
      columns: [
        {title: '序号',width: 80,dataIndex: 'index',
        customRender() {
            return arguments[2] + 1
        }
        },
        {
          title: '岗位',
          dataIndex: 'post',
          width: 150,
        },
        {
          title: '状态',
          dataIndex: 'status',
          width: 100,
          customRender: q => {
            return q ? '可用' : '不可用'
          }
        },
        {
          title: '操作',
          dataIndex: 'actions',
          width: 150,
          scopedSlots: { customRender: 'actions' }
        }
      ],
       loadData: async (parameter) => {
            return api.getPostList({}).then((res) => {
              return { code: 200, data: res.data }
            })
        },
      // loadData: async (q) => await api.list({ ...q, type: 25 })
    }
  },
  methods: {
    delList(request, callback, params, cancelCb, useMessage) {
    if (params) {
      this.$confirm({
        title: '确定是否删除数据？',
        okText: '是',
        okType: 'danger',
        cancelText: '否',
        onOk: async () => {
          let res = await request(params)
          let msg = '删除成功'
          if (useMessage) {
            msg = res.message
          }
          if (res.code == '0') {
            callback && await callback(res)
            this.$noticeSuccess(msg)
          }
        },
        onCancel: async () => {
          cancelCb && await cancelCb()
        }
      })
    } else {
      this.$message.warning('请选择要删除的数据')
    }
  },
    del(record) {
      this.delList(
        api.delPostList,
        (res) => {
          this.$refs.table.refresh()
        },
        { id: record.id },
        null
      )
    },
    add() {
      this.form = { status: 1 }
      this.visible = true
    },
    edit(record) {
      this.form = { ...record }
      this.form.status = this.form.status ? 1 : 0
      this.visible = true
    }
  }
}
</script>